package com.example.controller.backend;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.Result;
import com.example.handlers.BaseContext;
import com.example.service.ArticleFavoriteService;
import com.example.vo.ArticlePageVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台文章收藏控制器
 */
@RestController("backendArticleFavoriteController")
@RequestMapping("/article/favorite")
public class ArticleFavoriteController {

    @Resource
    private ArticleFavoriteService articleFavoriteService;

    /**
     * 获取用户收藏的文章列表
     */
    @GetMapping("/list")
    public Result<IPage<ArticlePageVO>> getFavoriteArticles(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            // 获取当前登录用户ID
            Integer userId = BaseContext.getCurrentId();
            if (userId == null) {
                return Result.fail("未登录");
            }

            // 获取收藏文章列表
            IPage<ArticlePageVO> articlePage = articleFavoriteService.getUserFavoriteArticles(userId, pageNum, pageSize);
            return Result.success(articlePage);
        } catch (Exception e) {
            return Result.fail("获取收藏文章列表失败: " + e.getMessage());
        }
    }
} 